<?php
namespace Tools;
	class Category{
		//组成一维数组
		static Public function unlimitedForLevel($cate,$html='------',$pid=0,$level=0){
			$arr=array();
			foreach ($cate as $v) {
				if($v['pid']==$pid){
					$v['level']=$level+1;
					$v['html']=str_repeat($html, $level);
					$arr[]=$v;
					$arr=array_merge($arr,self::unlimitedForLevel($cate,$html,$v[id],$level+1));
				}
			}
			return $arr;
		}
		//组成二维数组
		static Public function unlimitedForLayer($cate,$name='child',$pid=0){
			$arr=array();
			foreach ($cate as $v) {
				if($v['pid']==$pid){
					$v[$name]=self::unlimitedForLayer($cate,$name,$v['id']);
					$arr[]=$v;
				}
			}
			return $arr;
		}
		//从子级的id返回所有父级元素
		static Public function getParents($cate,$id){
			$arr=array();
			foreach ($cate as $v) {
				if ($v['id'] == $id) {
					$arr[]=$v;
					//递归传递的参数属性，决定返回的顺序，以下是从顶级往底级元素排列，反之则相反
					$arr=array_merge(self::getParents($cate,$v['pid']),$arr);
				}
			}
			return $arr;
		}
		//给出一个父级id返回所有子级分类的id
		static Public function getChildId($cate,$pid){
			$arr=array();
			foreach ($cate as $v) {
				if($v['pid']=$pid){
					$arr[]=$v['id'];
					$arr=array_merge($arr,self::getChildId($cate,$v['id']));

				}
			}
			return $arr;
		}
		//给出一个父级id返回所有子级分类
		static Public function getChlids($cate,$pid){
			$arr=array();
			foreach ($cate as $v) {
				if($v['id']=$pid){
					$arr[]=$v;
					$arr=array_merge($arr,self::getChlid($cate,$v['id']));
				}
			}
			return $arr;

		}

	}
?>